<?php
// Use environment variable for include path if available, otherwise use a relative path
if (getenv('PHP_INCLUDE_PATH')) {
    set_include_path(get_include_path() . PATH_SEPARATOR . getenv('PHP_INCLUDE_PATH'));
} else {
    set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../../');
}

require 'vendor/autoload.php';
require_once 'pkg/client/client.php';
require_once 'pkg/openapi/openapi.php';

use Client\Config;
use Client\SDKClient;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Openapi\QueryFunctionHeaders;
use Openapi\QueryFunctionRequest;
use Openapi\FunctionService;
use Openapi\InvokeFlowRequest; // 添加缺少的引入
use Openapi\CloudFlowClient; // 添加工作流服务类引入

// 加载环境变量
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/../../');
$dotenv->load();

// 检查环境变量是否设置
if (!getenv('CTYUN_AK') || !getenv('CTYUN_SK')) {
    echo 'Environment variables CTYUN_AK and CTYUN_SK must be set.';
    exit(1);
}

$cfg = new Config(
    "cf.fc-xinan1.ctyun.cn",
    getenv('CTYUN_AK'),
    getenv('CTYUN_SK'),
    "https",
    "POST"
);
// 创建http client
$c = new SDKClient($cfg);
$request = new QueryFunctionRequest();
$headers = new QueryFunctionHeaders([], "200000002368");
$region = '200000002368';

// 创建 function service
$cloudflowCli = new CloudFlowClient($c);
$workflowName = 'grey-0325';
$request = new InvokeFlowRequest($workflowName, '{ "aaa":"1" }'); // 替换为实际的参数值

// 发送请求
try {
    printf('example workflow invoke start with: %s', json_encode($request));
    $data = $cloudflowCli->invokeWorkflow($region, $request);
    printf('example workflow invoke return with: %s', json_encode($data));
} catch (RequestException $e) {
    echo 'example workflow invoke return with error.';
    echo $e->getMessage();
}
?>